package com.bocloud.cmp.dao.app.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.bocloud.cmp.dao.app.RegistryDao;
import com.bocloud.cmp.entity.app.resource.Registry;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;
/**
 * @author Zaney
 * @data:2017年3月9日
 * @describe:仓库Dao接口实现类
 */
@Repository("registryDao")
public class RegistryDaoImpl extends JdbcGenericDao<Registry, Long> implements RegistryDao {
	
	@Override
	public Registry query(Long id) throws Exception {
		String sql = "select * from registry where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Registry> list = this.list(Registry.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return this.get(Registry.class, id);
	}

	@Override
	public List<Registry> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from registry a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(Registry.class, sql, paramMap);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from registry a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Registry> registrys = this.list(Registry.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (Registry registry : registrys) {
			beans.add(new SimpleBean(registry.getId(), registry.getName()));
		}
		return beans;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from registry a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public boolean deleteById(Long id, Long userId) throws Exception {
		String sql = "update registry set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}
	@Override
	public List<Map<String, Object>> countInfo(Long userId) throws Exception {
		String sql = "SELECT SUM(case when property=0  then 1 else 0 end) public ,SUM(case when property=1 then 1 else 0 end) private "
				+ "FROM registry where is_deleted =0";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("menderId", userId);
		List<Map<String, Object>> list = this.list(sql, params);
		return list;
	}
	
	@Override
	public List<Registry> selectRegistryByIPWithPort(String address, Integer port) throws Exception {
		String sql = "select * from registry where is_deleted = 0 and address = :address and port = :port";
		Map<String, Object> params = MapTools.simpleMap("address", address);
		params.put("port", port);
		return this.list(Registry.class, sql,params);
	}
	
	@Override
	public List<Registry> selectAllRegistry() throws Exception {
		String sql = "select * from registry where is_deleted = 0 ";
		return this.list(Registry.class, sql);
	}

	@Override
	public Registry checkName(String name) throws Exception {
		String sql = "select * from registry where is_deleted = 0 and name = :name";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		List<Registry> list = this.list(Registry.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
